package com.tgu.home_action.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgu.home_action.dao.CaseMapper;
import com.tgu.home_action.dao.OldManMapper;
import com.tgu.home_action.pojo.Case;
import com.tgu.home_action.pojo.Family;
import com.tgu.home_action.pojo.OldMan;
import com.tgu.home_action.pojo.ResponseData;
import com.tgu.home_action.pojo.state.RabbitMq;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.OldManService;
import com.tgu.home_action.service.utils.AliYunService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Validated
@Service
public class OldManServiceImpl implements OldManService {
    public static final String entityPrefix = "oldMan_";
    @Autowired
    private OldManMapper oldManMapper;
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private AliYunService aliYunService;

    /**
     * 插入老人信息
     */
    @Override
    public ResponseData<OldMan> insertOldMan(OldMan oldMan, Family familyUser) {
        oldMan.setFamilyId(familyUser.getId());
        oldMan.setCreatedAt(new Timestamp(new Date().getTime()));
        oldMan.setUpdatedAt(new Timestamp(new Date().getTime()));
        oldManMapper.insertOldMan(oldMan);
        try {
            aliYunService.addFaceEntity(entityPrefix + oldMan.getId());
            aliYunService.addFace(oldMan.getIdentificationPhoto(), entityPrefix + oldMan.getId());
//            System.out.println(JSONArray.toJSON(oldMan.getLifePhoto()));
            JSONArray lifePhotoJSONArray = JSONArray.parseArray(oldMan.getLifePhoto());
            Object[] lifePhotos = lifePhotoJSONArray.toArray();
            for (int i = 0; i < 4 && i < lifePhotos.length; i++) {
                aliYunService.addFace((String) lifePhotos[i], entityPrefix + oldMan.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), oldMan);
    }

    /**
     * 查询老人信息
     */
    @Override
    public ResponseData<List<OldMan>> getOldMen(Family familyUser) {
        List<OldMan> oldMen = oldManMapper.getOldMenByFamilyId(familyUser.getId());
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), oldMen);
    }

    /**
     * 更改老人信息
     */
    @Override
    public ResponseData<Object> updateOldMan(OldMan oldMan, Family familyUser) {
        oldMan.setFamilyId(familyUser.getId());
        Boolean isSuccess = oldManMapper.updateOldMan(oldMan);
        if (isSuccess) {
            //判断是否有案件正在进行，有则推送信息变化
            Case nowCase = caseMapper.getIngCaseByOldManId(oldMan.getId());
            if (nowCase != null) {
                nowCase.setOldMan(oldMan);
                rabbitTemplate.convertAndSend(RabbitMq.CASE_EXCHANGE.getValue(), RabbitMq.CASE_INFORMATION_UPDATE.getValue(), nowCase);
            }
            if (oldMan.getLifePhoto() != null || oldMan.getIdentificationPhoto() != null) {
                try {
                    aliYunService.deleteFaceEntity(entityPrefix + oldMan.getId());
                    aliYunService.addFaceEntity(entityPrefix + oldMan.getId());
                    aliYunService.addFace(oldMan.getIdentificationPhoto(), entityPrefix + oldMan.getId());
                    System.out.println(JSONArray.toJSON(oldMan.getLifePhoto()));
                    JSONArray lifePhotoJSONArray = JSONArray.parseArray(oldMan.getLifePhoto());
                    Object[] lifePhotos = lifePhotoJSONArray.toArray();
                    for (int i = 0; i < 4 && i < lifePhotos.length; i++) {
                        aliYunService.addFace((String) lifePhotos[i], entityPrefix + oldMan.getId());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
        } else {
            return new ResponseData<>(ResponseState.OLD_MAN_NOT_EXIST.getMessage(), ResponseState.OLD_MAN_NOT_EXIST.getValue());
        }
    }

    /**
     * 删除老人信息
     */
    @Transactional
    @Override
    public ResponseData<Object> deleteOldMan(int oldManId, Family familyUser) {
        OldMan oldMan = new OldMan();
        oldMan.setId(oldManId);
        oldMan.setFamilyId(familyUser.getId());
        caseMapper.updateOldManId(oldMan.getId());
        Boolean isSuccess = oldManMapper.deleteOldMan(oldMan);
        if (isSuccess) {
            try {
                aliYunService.deleteFaceEntity(entityPrefix + oldManId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue());
        } else {
            return new ResponseData<>(ResponseState.OLD_MAN_NOT_EXIST.getMessage(), ResponseState.OLD_MAN_NOT_EXIST.getValue());
        }
    }
}
